home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1459.dms / var1459.adf / LowLevelGraphics / Example4.c < prev    next >
C/C++ Source or Header  |  1992-04-28  |  10KB  |  299 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: LowLevelGraphics            Tulevagen 22       */
  8. /* File:    Example4.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how to open two different ViewPorts on the */
  21. /* same display. The first ViewPort is in low resolution and use 32     */
  22. /* colours, while the second ViewPort is in high resolution and only    */
  23. /* use 2 colours.                                                       */
  24.  
  25.  
  26. #include <intuition/intuition.h>
  27. #include <graphics/gfxbase.h>
  28.  
  29.  
  30. /* ViewPort 1 */
  31. #define WIDTH1   320 /* 320 pixels wide.                              */
  32. #define HEIGHT1  150 /* 150 lines high.                               */ 
  33. #define DEPTH1     5 /* 5 BitPlanes should be used, gives 32 colours. */
  34. #define COLOURS1  32 /* 2^5 = 32                                      */
  35.  
  36. /* ViewPort 2 */
  37. #define WIDTH2   640 /* 640 pixels wide.                             */
  38. #define HEIGHT2   45 /* 45 lines high.                               */
  39. #define DEPTH2     1 /* 1 BitPlanes should be used, gives 2 colours. */
  40. #define COLOURS2   2 /* 2^1 = 2                                      */
  41.  
  42.  
  43. struct IntuitionBase *IntuitionBase;
  44. struct GfxBase *GfxBase;
  45.  
  46.  
  47. struct View my_view;
  48. struct View *my_old_view;
  49.  
  50.  
  51. /* ViewPort 1 */
  52. struct ViewPort view_port1;
  53. struct RasInfo ras_info1;
  54. struct BitMap bit_map1;
  55. struct RastPort rast_port1;
  56. UWORD color_table1[] =
  57. {
  58.   0x000, 0xFFF, 0xDDD, 0xBBB, 0x999, 0x777, 0x555, 0x333,
  59.   0xF00, 0xD00, 0xB00, 0x900, 0x700, 0x500, 0x300, 0x100,
  60.   0x0F0, 0x0D0, 0x0B0, 0x090, 0x070, 0x050, 0x030, 0x010,
  61.   0x00F, 0x00D, 0x00B, 0x009, 0x007, 0x005, 0x003, 0x001
  62. };
  63.  
  64.  
  65. /* ViewPort 2 */
  66. struct ViewPort view_port2;
  67. struct RasInfo ras_info2;
  68. struct BitMap bit_map2;
  69. struct RastPort rast_port2;
  70. UWORD color_table2[] = { 0x000, 0xFFF };
  71.  
  72.  
  73. void clean_up();
  74. void main();
  75.  
  76.  
  77. void main()
  78. {
  79.   UWORD *pointer;
  80.   int loop;
  81.   
  82.  
  83.  
  84.   /* Open the Intuition library: */
  85.   IntuitionBase = (struct IntuitionBase *)
  86.     OpenLibrary( "intuition.library", 0 );
  87.   if( !IntuitionBase )
  88.     clean_up( "Could NOT open the Intuition library!" );
  89.  
  90.   /* Open the Graphics library: */
  91.   GfxBase = (struct GfxBase *)
  92.     OpenLibrary( "graphics.library", 0 );
  93.   if( !GfxBase )
  94.     clean_up( "Could NOT open the Graphics library!" );
  95.  
  96.  
  97.  
  98.   /* Save the current View, so we can restore it later: */
  99.   my_old_view = GfxBase->ActiView;
  100.  
  101.  
  102.  
  103.   /* 1. Prepare the View structure, and give it a pointer to */
  104.   /*    the first ViewPort:                                  */
  105.   InitView( &my_view );
  106.   my_view.ViewPort = &view_port1;
  107.  
  108.  
  109.  
  110.   /* 2. Prepare the ViewPort structures, and set some important values: */
  111.  
  112.   /* ViewPort 1 */
  113.   InitVPort( &view_port1 );
  114.   view_port1.DWidth = WIDTH1;      /* Set the width.                */
  115.   view_port1.DHeight = HEIGHT1;    /* Set the height.               */
  116.   view_port1.DxOffset = 0;         /* X position.                   */
  117.   view_port1.DyOffset = 0;         /* Y position.                   */
  118.   view_port1.RasInfo = &ras_info1; /* Give it a pointer to RasInfo. */
  119.   view_port1.Modes = NULL;         /* Low resolution.               */
  120.   view_port1.Next = &view_port2;   /* Pointer to next ViewPort.     */
  121.  
  122.   /* ViewPort 2 */
  123.   InitVPort( &view_port2 );
  124.   view_port2.DWidth = WIDTH2;      /* Set the width.                */
  125.   view_port2.DHeight = HEIGHT2;    /* Set the height.               */
  126.   view_port2.DxOffset = 0;         /* X position.                   */
  127.   view_port2.DyOffset = HEIGHT1+5; /* Y position (5 lines under).   */
  128.   view_port2.RasInfo = &ras_info2; /* Give it a pointer to RasInfo. */
  129.   view_port2.Modes = HIRES;        /* High resolution.              */
  130.   view_port2.Next = NULL;          /* Last ViewPort in the list.    */
  131.  
  132.  
  133.  
  134.   /* 3. Get a colour map, link it to the ViewPort, and prepare it: */
  135.  
  136.   /* ViewPort 1 */
  137.   view_port1.ColorMap = (struct ColorMap *) GetColorMap( COLOURS1 );
  138.   if( view_port1.ColorMap == NULL )
  139.     clean_up( "Could NOT get a ColorMap!" );
  140.   /* Get a pointer to the colour map: */
  141.   pointer = (UWORD *) view_port1.ColorMap->ColorTable;
  142.   /* Set the colours: */
  143.   for( loop = 0; loop < COLOURS1; loop++ )
  144.     *pointer++ = color_table1[ loop ];
  145.  
  146.   /* ViewPort 2 */
  147.   view_port2.ColorMap = (struct ColorMap *) GetColorMap( COLOURS2 );
  148.   if( view_port2.ColorMap == NULL )
  149.     clean_up( "Could NOT get a ColorMap!" );
  150.   /* Get a pointer to the colour map: */
  151.   pointer = (UWORD *) view_port2.ColorMap->ColorTable;
  152.   /* Set the colours: */
  153.   for( loop = 0; loop < COLOURS2; loop++ )
  154.     *pointer++ = color_table2[ loop ];
  155.  
  156.  
  157.  
  158.   /* 4. Prepare the BitMap: */
  159.  
  160.   /* ViewPort 1 */
  161.   InitBitMap( &bit_map1, DEPTH1, WIDTH1, HEIGHT1 );
  162.   /* Allocate memory for the Raster: */ 
  163.   for( loop = 0; loop < DEPTH1; loop++ )
  164.   {
  165.     bit_map1.Planes[ loop ] = (PLANEPTR) AllocRaster( WIDTH1, HEIGHT1 );
  166.     if( bit_map1.Planes[ loop ] == NULL )
  167.       clean_up( "Could NOT allocate enough memory for the raster!" );
  168.     /* Clear the display memory with help of the Blitter: */
  169.     BltClear( bit_map1.Planes[ loop ], RASSIZE( WIDTH1, HEIGHT1 ), 0 );
  170.   }
  171.  
  172.   /* ViewPort 2 */
  173.   InitBitMap( &bit_map2, DEPTH2, WIDTH2, HEIGHT2 );
  174.   /* Allocate memory for the Raster: */ 
  175.   for( loop = 0; loop < DEPTH2; loop++ )
  176.   {
  177.     bit_map2.Planes[ loop ] = (PLANEPTR) AllocRaster( WIDTH2, HEIGHT2 );
  178.     if( bit_map2.Planes[ loop ] == NULL )
  179.       clean_up( "Could NOT allocate enough memory for the raster!" );
  180.     /* Clear the display memory with help of the Blitter: */
  181.     BltClear( bit_map2.Planes[ loop ], RASSIZE( WIDTH2, HEIGHT2 ), 0 );
  182.   }
  183.  
  184.  
  185.  
  186.   /* 5. Prepare the RasInfo structure: */
  187.  
  188.   /* ViewPort 1 */
  189.   ras_info1.BitMap = &bit_map1; /* Pointer to the BitMap structure.  */
  190.   ras_info1.RxOffset = 0;       /* The top left corner of the Raster */
  191.   ras_info1.RyOffset = 0;       /* should be at the top left corner  */
  192.                                 /* of the display.                   */
  193.   ras_info1.Next = NULL;        /* Single playfield - only one       */
  194.                                 /* RasInfo structure is necessary.   */
  195.  
  196.   /* ViewPort 2 */
  197.   ras_info2.BitMap = &bit_map2; /* Pointer to the BitMap structure.  */
  198.   ras_info2.RxOffset = 0;       /* The top left corner of the Raster */
  199.   ras_info2.RyOffset = 0;       /* should be at the top left corner  */
  200.                                 /* of the display.                   */
  201.   ras_info2.Next = NULL;        /* Single playfield - only one       */
  202.                                 /* RasInfo structure is necessary.   */
  203.  
  204.  
  205.  
  206.   /* 6. Create the display: */
  207.   MakeVPort( &my_view, &view_port1 ); /* Prepare ViewPort 1 */
  208.   MakeVPort( &my_view, &view_port2 ); /* Prepare ViewPort 2 */
  209.   MrgCop( &my_view );
  210.  
  211.  
  212.  
  213.   /* 7. Prepare the RastPort, and give it a pointer to the BitMap. */
  214.  
  215.   /* ViewPort 1 */
  216.   InitRastPort( &rast_port1 );
  217.   rast_port1.BitMap = &bit_map1;
  218.  
  219.   /* ViewPort 2 */
  220.   InitRastPort( &rast_port2 );
  221.   rast_port2.BitMap = &bit_map2;
  222.  
  223.  
  224.  
  225.   /* 8. Show the new View: */
  226.   LoadView( &my_view );
  227.  
  228.  
  229.  
  230.   /* Set the draw mode to JAM1. FgPen's colour will be used. */
  231.   SetDrMd( &rast_port1, JAM1 );
  232.   SetDrMd( &rast_port2, JAM1 );
  233.  
  234.   /* Set FgPen's colour to 1 (white). */
  235.   SetAPen( &rast_port2, 1 );
  236.   /* Draw some pixels in the second ViewPort: */
  237.   for( loop = 0; loop < 500; loop++ )
  238.     WritePixel( &rast_port2, rand() % WIDTH2, rand() % HEIGHT2 );
  239.  
  240.   /* Print some text into the second ViewPort: */
  241.   Move( &rast_port2, 0, 10 );
  242.   Text( &rast_port2, "This text is written on a single high resolution BitMap. The ViewPort above use ", 80 );
  243.   Move( &rast_port2, 0, 20 );
  244.   Text( &rast_port2, "a 32-colour low resolution BitMap.                                              ", 80 );
  245.  
  246.   /* Draw 10000 pixels in seven different colours, randomly. */ 
  247.   for( loop = 0; loop < 10000; loop++ )
  248.   {
  249.     /* Set FgPen's colour (1-31, 0 used for the the background). */
  250.     SetAPen( &rast_port1, rand() % (COLOURS1-1) + 1 );
  251.     /* Write a pixel somewere on the display: */
  252.     WritePixel( &rast_port1, rand() % WIDTH1, rand() % HEIGHT1 );
  253.   }
  254.   
  255.  
  256.  
  257.   /* 9. Restore the old View: */
  258.   LoadView( my_old_view );
  259.  
  260.  
  261.   /* Free all allocated resources and leave. */
  262.   clean_up( "THE END" );
  263. }
  264.  
  265.  
  266. /* Returns all allocated resources: */
  267. void clean_up( message )
  268. STRPTR message;
  269. {
  270.   int loop;
  271.  
  272.   /* Free automatically allocated display structures: */
  273.   FreeVPortCopLists( &view_port1 );
  274.   FreeVPortCopLists( &view_port2 );
  275.   FreeCprList( my_view.LOFCprList );
  276.   
  277.   /* Deallocate the display memory, BitPlane for BitPlane: */
  278.   for( loop = 0; loop < DEPTH1; loop++ )
  279.     if( bit_map1.Planes[ loop ] )
  280.       FreeRaster( bit_map1.Planes[ loop ], WIDTH1, HEIGHT1 );
  281.   for( loop = 0; loop < DEPTH2; loop++ )
  282.     if( bit_map2.Planes[ loop ] )
  283.       FreeRaster( bit_map2.Planes[ loop ], WIDTH2, HEIGHT2 );
  284.  
  285.   /* Deallocate the ColorMap: */
  286.   if( view_port1.ColorMap ) FreeColorMap( view_port1.ColorMap );
  287.   if( view_port2.ColorMap ) FreeColorMap( view_port2.ColorMap );
  288.  
  289.   /* Close the Graphics library: */
  290.   if( GfxBase ) CloseLibrary( GfxBase );
  291.  
  292.   /* Close the Intuition library: */
  293.   if( IntuitionBase ) CloseLibrary( IntuitionBase );
  294.  
  295.   /* Print the message and leave: */
  296.   printf( "%s\n", message ); 
  297.   exit();
  298. }
  299.